#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_select.py
# author:wmf
# datetime:2021/5/23 20:16
# software: PyCharm
'''
this is function description
'''
# import module your need
from homework10.homework10_2 import db


# 根据班级ID查询
def dbdisplay_byclassID(classID):
    cursor = db.cursor()
    sql = '''select * from test.student
             where classID = '%s'
    ''' % classID
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            student_id = row[0]
            student_name = row[1]
            student_age = row[2]
            student_sex = row[3]
            student_class = row[4]
            student_isdeleted = row[5]
            student_datetime = row[6]
            print("student_id = %s,student_name = %s,student_age = %s,student_class = %s,student_sex = %s,"
                  "student_isdeleted = %s,student_datetime = %s" % (
                      student_id, student_name, student_age, student_class,
                      student_sex, student_isdeleted, student_datetime))
        print("查询成功!")
        db.close()
    except:
        print("Error: unable to fetch data")


# 用户填写sql语句查询
def dbdisplay_bySQL(sql):
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            student_id = row[0]
            student_name = row[1]
            student_age = row[2]
            student_sex = row[3]
            student_class = row[4]
            student_isdeleted = row[5]
            student_datetime = row[6]
            print("student_id = %s,student_name = %s,student_age = %s,student_class = %s,student_sex = %s,"
                  "student_isdeleted = %s,student_datetime = %s" % (
                      student_id, student_name, student_age, student_class,
                      student_sex, student_isdeleted, student_datetime))
        print("查询成功!")
        db.close()
    except:
        print("Error: unable to fetch data")


if __name__ == "__main__":
    # dbdisplay_bySQL(sql='select * from test.student where age > 19')
    dbdisplay_byclassID(1)